<?php
namespace Forum\Controller;
use Think\Controller;
class ForumController extends CommonController {
    public function index(){
    }
    
    /*
     * 圈子列表
     */
    public function lists()
    {
        
        if(IS_POST)
        {
            /*
             * post提交添加圈子
             */
            
            
            $DB = M("forum_forum");
            
            $name = I("post.name");
            
            $data['name']=$name;
            
            $data['ctime']=I("server.REQUEST_TIME");
            
            $id = $DB->add($data);
            
            if($id)
            {
                $this->success("添加成功",U("modify",array('fid'=>$id)));
            }
            else
            {
                $this->error("操作错误");
            }
            
        }
        else
        {
            /*
            * 
            * 加载圈子
            * 
            * 连接数据库
            * 选择范围参数设置
            * 
            * 
            */
           $DB = M("forum_forum");

   //        $map
           $res = $DB->where($map)->select();
           foreach ($res as $key => $value) {
               # code...
                $res[$key]['parent_forum_name'] = M("forum_parent_forum")->where(array('id'=>$value['parentid']))->getField('name');
                //获取管理员的资料
                $res[$key]['manager_info'] = $this->_get_forum_manager($value['fid']);
           }

           $this->assign("results", $res);

           /*
            * 加载圈子父目录板块
            */

           $DB_borad = M("forum_parent_forum");
           $res_borad = $DB_borad->select();

           $this->assign("borads", $res_borad);


           $this->display();
        }
        
    }
    
    
    /*
     * 圈子修改
     * 修改内容有: 父目录\名字\图标\是否显示\简介\
     * 
     * 
     */
    public function modify() {
        
        if(IS_POST)
        {
            $DB = M("forum_forum");
            $fid = I("post.fid");
            $DB->fid = $fid;
            /*
             * 修改的功能
             */
            if(!empty($_FILES['logo']['tmp_name']))
            {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     31457280 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
//                $upload->rootPath  =      'Public/'; // 设置附件上传目录
                $upload->savePath  =      'forum/'; // 设置附件上传目录
                
                // 上传文件 
                $info   =   $upload->uploadOne($_FILES['logo']);

                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                } 
                $DB->logo="/".$upload->rootPath.$info['savepath'].$info['savename'];
            }
            
            $DB->parentid=I("post.borad");
            $DB->name=I("post.name");
            $DB->status=I("post.status");
            $DB->short_description=I("post.short_description");
            $DB->description=I("post.description");
            
            if($DB->save())
            {
                $this->success("修改成功");
            }
            else
            {
                $this->error("操作失败");
            }
            
        }
        else
        {
            $fid = I("get.fid");
        
            $DB = M("forum_forum");

            $res = $DB->find($fid);

            //获取管理员的资料
            $res['manager_info'] = $this->_get_forum_manager($fid);


            $this->assign("result", $res);
            
            /*
            * 加载圈子父目录板块
            */

           $DB_borad = M("forum_parent_forum");
           $res_borad = $DB_borad->select();

           $this->assign("borads", $res_borad);
           
            $this->display();  
        }
        
    }
    
    /*
     * 添加管理员
     */
    public function add_manager() {
        $userid = I("post.userid");
        $fid = I("post.fid");
        
        $data['userid']=$userid;
        $data['fid']=$fid;
        $data['ctime'] = I("server.REQUEST_TIME");
        
        $DB = M("forum_manager_relationship");
        $id = $DB->add($data);
        if($id)
        {
            $this->success("添加成功",U('modify',array('fid'=>$fid)));
        }
        else
        {
            $this->error("操作失败",U('modify',array('fid'=>$fid)));
        }
        
    }
    
    /*
     * 删除管理员
     */
    public function del_manager() {
        $id = I("get.id");
        $DB = M("forum_manager_relationship");
        
        if($DB->delete($id))
        {
            $this->success("删除成功");
        }
    }

    

    //通过fid 获取板块的管理员资料
    public function _get_forum_manager($fid)
    {
        $map['fid']=$fid;
        $DB = M("forum_manager_relationship");
        $res = $DB->where($map)->select();
        $result_str = "";
        foreach ($res as $key => $value) {
            # code...
            $user_nickname = M("forum_users")->where(array('userid'=>$value['userid']))->getField('nickname');
            $res[$key]['nickname'] = $user_nickname;
            $result_str .=$user_nickname.",";
        }

        $result['mangers_str'] = $result_str;
        $result['managers'] = $res;

        return $result;

    }
}